package org.neurone.action.application;

import java.util.List;

import org.neurone.action.BaseActionSupport;
import org.neurone.application.ApplicationMgr;
import org.neurone.application.impl.ApplicationFaceLocal;
import org.neurone.application.impl.ApplicationFaceLocalFactory;
import org.neurone.context.MessageConst;
import org.neurone.domain.Application;
import org.neurone.form.ApplicationForm;

public class ApplicationListAction extends BaseActionSupport {
	private List<Application> appList;
	private ApplicationForm	app;
	private Long appid;
	
	private ApplicationMgr appMgr;
	private ApplicationFaceLocalFactory appFactory;
	
	private boolean modType;

	public List<Application> getAppList() {
		return appList;
	}

	public void setAppList(List<Application> appList) {
		this.appList = appList;
	}

	public ApplicationForm getApp() {
		return app;
	}

	public void setApp(ApplicationForm app) {
		this.app = app;
	}


	public Long getAppid() {
		return appid;
	}

	public void setAppid(Long appid) {
		this.appid = appid;
	}

	public ApplicationMgr getAppMgr() {
		return appMgr;
	}

	public void setAppMgr(ApplicationMgr appMgr) {
		this.appMgr = appMgr;
	}

	public ApplicationFaceLocalFactory getAppFactory() {
		return appFactory;
	}

	public void setAppFactory(ApplicationFaceLocalFactory appFactory) {
		this.appFactory = appFactory;
	}

	public boolean isModType() {
		return modType;
	}

	public void setModType(boolean modType) {
		this.modType = modType;
	}
	
	
	public String defaultShow()
	{
		this.modType = false;
		return showList();
	}
	private String showList()
	{
		this.appList = appMgr.findAll(0, 0);
		return success();
	}
	
	public String prepareMod()
	{
		this.modType = true;
		if(appid != null)
		{
			if(this.app == null)
			{
				ApplicationFaceLocal a = this.appFactory.getNewApplicationFace(this.appid);
				if(a.isPersistentAvailable())
				{

					this.app = new ApplicationForm();

					this.app.setApplicationDescription(a.getApplicationDescription());
					this.app.setApplicationID(a.getApplicationID());
					this.app.setApplicationKey(a.getApplicationKey());
					this.app.setApplicationName(a.getApplicationName());
					this.app.setApplicationType(a.getApplicationType());
					this.app.setCallbackUrl(a.getCallbackUrl());
					this.app.setIcon(a.getIcon());
					this.app.setPluginType(a.getPluginType());
					this.app.setServerIP(a.getServerIP());
					this.app.setTags(a.getTags());
					this.app.setVisibleLevelKey(a.getVisibleLevelKey());

				}else
				{
					this.addActionError(MessageConst.NO_SUCH_APPLICATION);
					return ERROR;
				}
			}
		}
		else
		{
			this.app = null;
		}
		
		return showList();		
	}
	
	private String newApp()
	{
		if(this.app !=null)
		{
			this.appid = appMgr.newApplication(this.app);
			return success();
		}
		else
		{
			return INPUT;
		}
	}
	private String updateApp()
	{
		if(this.app !=null)
		{
			ApplicationFaceLocal a = this.appFactory.getNewApplicationFace(this.appid);
			
			a.setApplicationDescription(this.app.getApplicationDescription());
			a.setApplicationKey(this.app.getApplicationKey());
			a.setApplicationName(this.app.getApplicationName());
			a.setApplicationType(this.app.getApplicationType());
			a.setCallbackUrl(this.app.getCallbackUrl());
			a.setIcon(this.app.getIcon());
			a.setPluginType(this.app.getPluginType());
			a.setServerIP(this.app.getServerIP());
			a.setTags(this.app.getTags());
			a.setVisibleLevelKey(this.app.getVisibleLevelKey());
			
			a.flush();
			
			return success();
		}
		else
		{
			return INPUT;
		}
	}
	
	public String modApp()
	{
		if(appid !=null)
		{
			return updateApp();
		}
		else
		{
			return newApp();
		}
	}	
	public String deleteUser()
	{
		if(appid != null)
		{
			this.appMgr.delete(appid);
		}
		return showList();
	}	
}
